//右击表头弹出菜单
//需要改进:表头名称不能有html标签元素，必须是纯文本;只能在IE使用，其它浏览器没兼容
function rightClickFn(grid,columnIndex,scope){
    scope.preventDefault();
	var gridCM = grid.getColumnModel();
	var gridDS = grid.getStore(); 
	var e = event.srcElement?event.srcElement:event.target;
	if(document.getElementById("edit") != null)
	{
		this.focus();	
		return false;
	}
	
	var rightClick = new Ext.menu.Menu({ 
	id:'rightClickMenu',  
	items: [ 
	  { 
		  id: 'addRow', 
		  handler:function(){addRow(grid,e,gridCM,gridDS);}, 
		  text: '插入行（上方）',
		  icon: getRootPath()+"/images/icon/charu_1.png"
	  }, 
	  { 
		  id: 'deleteRow', 
		  handler: function(){deleteRow(grid,e,gridCM,gridDS);}, 
		  text: '删除行',
		  icon: getRootPath()+"/images/icon/delete_1.gif"
	  } ,
	  { 
		  id: 'rightMergeCell', 
		  handler: function(){rightMergeCell(grid,e,gridCM,gridDS);}, 
		  text: '右合并单元格',
		  icon: getRootPath()+"/images/icon/hebing_1.gif" 
	  } ,
	  { 
		  id: 'horizontalSplitCell', 
		  handler: function(){horizontalSplitCell(grid,e,gridCM,gridDS);}, 
		  text: '拆分单元格',
		  icon: getRootPath()+"/images/icon/chaifen_1.gif" 
	  } ,
	  { 
		  id: 'editColumnName', 
		  handler: function(){editColumnName(grid,e,gridCM,gridDS);}, 
		  text: '编辑列名',
		  icon: getRootPath()+"/images/icon/bianji_1.png"
	  } 
	] 
	}); 
	var rowIndex = getGridRowIndex(e);
	if(rowIndex == 0)
	{
		rightClick.items.get('deleteRow').hidden = true;
		rightClick.items.get('rightMergeCell').hidden = true;
		rightClick.items.get('horizontalSplitCell').hidden = true;
		rightClick.items.get('editColumnName').hidden = true;
	}
	else
	{
		var rowsSize = gridCM.rows.length ;
		var index = rowsSize - rowIndex;
		var clickCellIndex = getClickCellIndex(e);
		var clickColumnSize = gridCM.rows[index].length;		//计算所点击行的列数
		if(clickCellIndex == clickColumnSize - 1)
		{
			rightClick.items.get('rightMergeCell').disabled = true;
		}
		if(gridCM.rows[index][clickCellIndex].colspan == 1)
		{
			rightClick.items.get('horizontalSplitCell').disabled = true;
		}
	}
	rightClick.showAt(scope.getXY());
	rightClick.on("beforehide",function(e){
		this.destroy();
	});
}
//双击表头修改列名
function headerDbclickFn(grid,columnIndex,scope)
{
	var e = event.srcElement?event.srcElement:event.target;
	if(document.getElementById("edit") != null)
	{
		return false;
	}
	var gridCM = grid.getColumnModel();
	var gridDS = grid.getStore();
	editColumnName(grid,e,gridCM,gridDS);
}
//单击表头时获取焦点
function headerClickFn(grid,columnIndex,scope)
{
	var e = event.srcElement?event.srcElement:event.target;
	e.focus();
}
//插入行
function addRow(grid,e,gridCM,gridDS)
{
	//var clone = tableNode.cloneNode(true);
	//tableNode.parentNode.insertBefore(clone,tableNode);
	var rowIndex = getGridRowIndex(e);
	var rowsSize = gridCM.rows.length ;
	var index = 0;//插入行的位置
	if(rowIndex == 0)
	{
		index = rowsSize;
	}
	else if(rowIndex >0)
		{			
			index = rowsSize - rowIndex;
		}
	//插入空白行
	var spaceRow = [];
	var columnSize = gridCM.columns.length;
	for(var i = 0;i < columnSize;i++)
	{
		spaceRow[i] = {header:'',align:'center'};
	}
	gridCM.rows.splice(index,0,spaceRow);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}

//删除行
function deleteRow(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	var rowsSize = gridCM.rows.length ;
	if(rowIndex <= 0)
	{
		return false;
	}
	var index = rowsSize - rowIndex;  //计算要删除的行序号
	gridCM.rows.splice(index,1);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}
//向右合并单元格
function rightMergeCell(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	if(rowIndex <= 0)
	{
		return false;
	}
	var rowsSize = gridCM.rows.length ;
	var index =  rowsSize - rowIndex;  //计算要合并的行序号
	var clickCellIndex = getClickCellIndex(e);
	var clickColumnSize = gridCM.rows[index].length;		//计算所点击行的列数
	if(clickCellIndex == clickColumnSize - 1)
	{
		return false;
	}
	var nextColspan = gridCM.rows[index][clickCellIndex+1].colspan ;	//得到点击列的右边列的colspan
	var nextHeader = gridCM.rows[index][clickCellIndex+1].header ;	//得到点击列的右边列的header
	gridCM.rows[index][clickCellIndex].colspan = gridCM.rows[index][clickCellIndex].colspan + nextColspan;
	gridCM.rows[index][clickCellIndex].header = gridCM.rows[index][clickCellIndex].header + nextHeader;
	gridCM.rows[index].splice(clickCellIndex+1,1);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}
//编辑列名
function editColumnName(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	if(grid.enableHeadEdit && rowIndex == 0)
	{
		return false;
	}
	var newInput = "<input id='edit' style='width:100%' onblur = '' value="+e.innerText+">";
    var dh = Ext.DomHelper;
	e.innerText = "";
	dh.append(e,newInput);
	var editElement = document.getElementById("edit");
	editElement.focus();
	editElement.focus();
	editElement.onblur=function(){inputToLable(grid,e,rowIndex,gridCM,gridDS);};
}
function inputToLable(grid,e,rowIndex,gridCM,gridDS)
{
	var text = document.getElementById("edit").value;
	var rowsSize = gridCM.rows.length ;
	var index =  rowsSize - rowIndex;  //计算要合并的行序号
	var clickCellIndex = getClickCellIndex(e);
	if(rowIndex == 0){
		//第一行普通表头
		gridCM.setColumnHeader(clickCellIndex,text);
	}else{
		//复杂表头
		gridCM.rows[index][clickCellIndex].header = text ;
		grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
	}
}
//横向拆分单元格
function horizontalSplitCell(grid,e,gridCM,gridDS)
{
	var rowIndex = getGridRowIndex(e);
	if(rowIndex <= 0)
	{
		return false;
	}
	var rowsSize = gridCM.rows.length ;
	var index =  rowsSize - rowIndex;  //计算要合并的行序号
	var clickCellIndex = getClickCellIndex(e);
	var colspan = gridCM.rows[index][clickCellIndex].colspan;
	if(colspan == 1)
	{
		return false;
	}
	gridCM.rows[index][clickCellIndex].colspan = 1;
	var splitCell = {header:'',align:'center',colspan:''};
	splitCell.colspan = colspan - 1;
	gridCM.rows[index].splice(clickCellIndex+1,0,splitCell);
	grid.reconfigure(gridDS, new Ext.grid.ColumnModel(gridCM));
}
//获取所点击的行序号
function getGridRowIndex(e)
{
	var tableNode = e.parentNode.parentNode.parentNode.parentNode;
	var divNode = tableNode.parentNode;
	var rowIndex = 0;	
	for(var i = 0 ,len = divNode.childNodes.length;i < len;i++)
	{
		if(divNode.childNodes[i] == tableNode)
		{
			rowIndex = len - i -1;
			break;
		}
	}
	return rowIndex;
}
//计算点击列在当前行数组(cm.rows)中的序号
function getClickCellIndex(e)
{
	var clickCellIndex = 0; 
	var theadNode = e.parentNode.parentNode;
	for(var n = 0 ,len = theadNode.childNodes.length;n < len; n++)
	{
		if(e.parentNode == theadNode.childNodes[n])
		{
			clickCellIndex = n;
			break;
		}
	}
	return clickCellIndex;
}
//项目根路径
function getRootPath(){
	var strFullPath=window.document.location.href;
	var strPath=window.document.location.pathname;
	var pos=strFullPath.indexOf(strPath);
	var prePath=strFullPath.substring(0,pos);
	var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
	return(prePath+postPath);
}